#include "LinkStack.h"
#include <stdlib.h>
#include <stdio.h>

//链式栈
struct LinkStack
{
    DLlist list;
};

LStack *InitLStack()
{
    LStack *s = (LStack*)malloc(sizeof(LStack));
    if(s == NULL)
        return NULL;
    //栈的初始化即初始化一个双向链表
    InitDLlist(&s->list);
    return s;
}

void SPush(LStack *s, ElementType element)
{
    InsertDLlistTail(&s->list,element);
}

void SPop(LStack *s)
{
    //出栈不等于删除
    RemoveByIndex(&s->list,s->list.len - 1,NULL);
}

ElementType *GetSTop(LStack *s)
{
    if(s->list.len == 0)
    {
        printf("栈为空!\n");
        return NULL;
    }
    return &s->list.tail->data;
}

bool IsStackEmpty(LStack *s)
{
    return s->list.len == 0;
}

void TravelLStack(LStack *s,void(*funcPtr)(ElementType))
{
    TravelDLlist(&s->list,funcPtr);
}


void ClearLStack(LStack *s, void (*clearPtr)(ElementType))
{
    // 清除栈即清除链表
    ClearDLlist(&s->list,clearPtr);
}

int GetStackLen(LStack *s)
{
    return s->list.len;
}
